import re

import requests
def getandsavevideo():
    url='https://www.kuaishou.com/rest/v/profile/feed'
    data={
      "user_id": "3xy9zdr59dn5qe2",
      "pcursor": "",
      "page": "profile"
    }

    headers = {
        'content-type': 'application/json',
        'Cookie':'kpf=PC_WEB; clientid=3; did=web_975a97375de0b8116aab9a332a49adc1; kwpsecproductname=kuaishou-vision; kwpsecproductname=kuaishou-vision; userId=3356712789; kuaishou.server.webday7_st=ChprdWFpc2hvdS5zZXJ2ZXIud2ViZGF5Ny5zdBKwASURjC-Xw0LHrEH2_D-0ytrZ3S-1kaiuK6WXxtgVcXO50CFShn4Kk3Tq5rDvO0Yzm9iAMoti06_DLp-VEJwNuBYO_3z2Q0v4e9xnbkG4aFD41uxd8Zi0K8hlms_8RJkZ7d0XB02o4S9DL2Emhx4y7FzFj4KiVC-8uQE_irJZMQZ84Dl_Co1mEjaN78_mjiB4xvzgXzGx7jBnx9Ms9KSKyTOogV8-JMKrjuAu8oK92QbeGhI8tsmuBlpRuNU4fo_gMPQVxSUiIKLC6Ky0EzJqASFRr6AuqYJvI1j34CuKJ-wt5qwvK0BPKAUwAQ; kuaishou.server.webday7_ph=488c67d9abdc2cdf91b70d0b6c988abe5228; kwssectoken=LI+G9CBMtHnDweW5pH63VHMyUdcOFRCAVRo7zWe3vgh9A/DlxK+GpqdPiD7gX6c3sMEMh92P12CEbq0d0+EWLA==; kwscode=1dc1737b22e8c2004d63385ab54d1899dcef8852af36206501063e713e9d2d6c; kwfv1=PnGU+9+Y8008S+nH0U+0mjPf8fP08f+98f+nLlwnrIP9+Sw/ZFGfzY+eGlGf+f+e4SGfbYP0QfGnLFwBLU80mYG9zjPeLAP/rIP0mj8Br9wBQY8fpS+fPhG/ch+ApS+0cAPnp0wBPFP0H9PAmf8/zjP0ZFGfrM8eGEPeH98eYfwBrhPBQfPnrAGApj+/40PnzS+0bDP9rhP/DUwnbj8/pY+Z==; kwssectoken=9Sygrdf5EKu9Med5eMuOW29rRLFGpod/08wImU+h2U8MeJMleZ5D+CefybXXEXem8VKikm57y1ryJVhCO+8UCA==; kwscode=d41ceef34ccc29476f5f3078333261cc153215dac112cb22233a3653b8e25439; ktrace-context=1|MS44Nzg0NzI0NTc4Nzk2ODY5LjY3NDEzOTk2LjE3NjMzNTEwNzM1OTQuMzA4NzA4MjA=|MS44Nzg0NzI0NTc4Nzk2ODY5LjY0NjQ1NDQzLjE3NjMzNTEwNzM1OTUuMzA4NzA4MjE=|0|webservice-user-growth-node|webservice|true|src-Js; kpn=KUAISHOU_VISION:',
        'Host': 'www.kuaishou.com',
        'Origin': 'https://www.kuaishou.com',
        # 换抓取主页地址
        'Referer': 'https://www.kuaishou.com/profile/3xy9zdr59dn5qe2',
        'Sec-Ch-Ua':'"Chromium";v="136", "Google Chrome";v="136", "Not.A/Brand";v="99"',
        'Sec-Ch-Ua-Mobile':'?0',
        'Sec-Ch-Ua-Platform':'"WINDOWS"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'
    }

    response = requests.post( url=url,json=data,headers=headers)
    # print(response.text)

    feeds=response.json()['feeds']

    for feed in feeds:
        #获取标题
        caption=feed['photo']['caption']
        #获取视频地址
        photo_url=feed['photo']['photoUrls'][0]['url']

        id=feed['photo']['id']
        print(caption+id,photo_url)
        videoUrl=requests.get(url=photo_url).content
        videoname=remove_special_characters(caption)
        with open ('video\\'+videoname+id+'.mp4','wb') as f:
            f.write(videoUrl)

def remove_special_characters(oldstring):
    newString=re.sub(r'[^\w\u4e00-\u9fff]','',oldstring)
    print(newString)
    return newString

if __name__ == '__main__':
    getandsavevideo()
    # remove_special_characters("第3集 | 梦醒大明03.mp4 #虾仁动画#沙雕动画3x79xnr8uyyjeda")